package com.ea.eadp.nexus.proxy.tests.identity;

import com.ea.eadp.nexus.test.common.*;
import com.ea.eadp.nexus.test.common.ApiClients.NexusConnectApi;
import com.ea.eadp.nexus.test.common.ApiClients.NexusGatewayApi;
import com.ea.eadp.nexus.test.common.ApiClients.NucleusApi;
import com.ea.nucleus.caseproperties.CaseProperties;
import com.ea.nucleus.caseproperties.Priority;
import com.ea.nucleus.caseproperties.Release;
import com.ea.nucleus.caseproperties.ReleaseFeatures.Aug2014;
import com.ea.nucleus.caseproperties.Status;
import com.esotericsoftware.minlog.Log;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yimingcao on 7/30/2014.
 */
public class MultiUserSearch {

    private static String signupPassword = "1234qwER";

    @CaseProperties(
            priority = Priority.Dailies,
            release = Release.Aug2014,
            status = Status.Enable,
            features = Aug2014.APICallReturningCountryInfoForMultiUsers,
            owner = "yimingcao",
            description = "more and detail test implemented in eaid-pid-integration"
    )
    @Test(groups = {"regression","intregression"})
    public void testMultiUserSearchIncludingMinorUser() throws Exception {
        String clientId = "EASFC-SERVER";
        String secret = "EASFC-SERVER_secret";

        String signupEmail = TestUtil.getRandomEmail();
        NucleusApi.instance().postPidAndPersonaIfNotExisted(signupEmail, signupPassword);

        String normalUserId = NucleusApi.instance().postPid(TestUtil.getRandomEmail(), signupPassword).split("/")[2];

        String minorUserId = NucleusApi.instance().postMinorPid(TestUtil.getRandomEmail(), "rtpass").split("/")[2];

        List<String> pidList = new ArrayList<String>();
        pidList.add(normalUserId);
        pidList.add(minorUserId);

        String pidListStr = TestUtil.connectStringByComma(pidList);

        /* do not remove this
        AccessToken userToken = NexusConnectApi.instance().getAccessTokenWithScope(null, signupEmail, signupPassword);
        String code = NexusConnectApi.instance().getCodeByToken(userToken, clientId);
        AccessToken token = NexusConnectApi.instance().getTokenByCodeWithClientSecret(code, clientId, secret);
        */

        AccessToken token = NexusConnectApi.instance().getTokenByUserPwdWithClientSecret(signupEmail, signupPassword, clientId, secret, null);

        List<NameValuePair> additionalHeaders = new ArrayList<NameValuePair>();
        additionalHeaders.add(new BasicNameValuePair("X-Include-Underage", "true"));

        HashMap<String, String> context = new HashMap<String, String>();
        context.put("pidlist", pidListStr);

        String returnMessage = NexusGatewayApi.instance().get(NexusGatewayApiEnum.pid_multisearch_get, context, token, additionalHeaders);

        Log.info(returnMessage);

        for(String x : pidList) {
            Assert.assertTrue(returnMessage.contains(x));
        }
    }
}
